# -*- coding: utf-8 -*-

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class MainWindow(QWidget):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent=parent)

        layout = QVBoxLayout()
        self.btn = QPushButton("加载图片")
        self.btn.clicked.connect(self.getFile)
        layout.addWidget(self.btn)

        self.le = QLabel("")
        layout.addWidget(self.le)

        self.btn2 = QPushButton("加载文本文件")
        self.btn2.clicked.connect(self.getFile2)
        layout.addWidget(self.btn2)

        self.content = QTextEdit()
        layout.addWidget(self.content)

        self.setLayout(layout)
        self.setWindowTitle("File Dialog示例")

    def getFile(self):
        f, _ = QFileDialog.getOpenFileName(self, "Open File", "D:\\", "Image files (*.jpg *.png)")
        self.le.setPixmap(QPixmap(f))

    def getFile2(self):
        dlg = QFileDialog()
        dlg.setFileMode(QFileDialog.AnyFile)
        dlg.setFilter(QDir.Files)

        if dlg.exec():
            files = dlg.selectedFiles()
            f = open(files[0], 'r')

            with f:
                data = f.read()
                self.content.setText(data)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec())